<template>
	<view class="content fofa poRel">
		<tabnav title="合同详情" :col="back"></tabnav>
		<view v-if="list">
			<view class="wid100" :style="'background-color: '+back+';'" style="height: 300rpx;">
				<view class="wid90 mar coFFF">
					<view class="dis disAl foSi40">
						<view style="width: 20%;" class="dis disAl disJuC">
							<!--  -->
							<image src="../../static/wc/saoma.png" style="width: 80rpx;height: 80rpx;"></image>
						</view>
						<view>{{list.tips}}</view>
					</view>
					<view class="dis disAl foSi30 mar-top10">
						<view style="width: 20%;"></view>
						<view>剩余时间: {{list.deadline}}</view>
					</view>
				</view>
			</view>
			<view class="wid90 mar bacFFF borRad20" style="margin-top: -120rpx;">
				<view class="wid90 mar">
					<view style="height: 60rpx;"></view>
					<view class="hei80 dis disAl disJuC foSi35 fowe600">{{list.contract_name}}</view>
					<view class="" style="text-align: center;font-size: 12px;color: #777;">
						扫描二维码签署合同
					</view>
					<view class="dis disJuC mar-top40">
						<image :src="erImage" style="width: 300rpx;height: 300rpx;"></image>
					</view>
					<view class="" style="text-align: center;font-size: 12px;color: #0400ff;margin-top: 10rpx;"
						@click="saveewm">
						点击下载二维码
					</view>
					<view style="border-bottom: 1rpx solid #e6dede;height: 60rpx;"></view>
					<view v-for="(item,index) in list.recipient" :key="index" class="hei80 dis disAl foSi30 fowe600"
						style="border-bottom: 1rpx solid #e6dede;">
						<view style="width: 5%;"></view>
						<view>签署方</view>
						<view class="mar-left30">{{item.roleName?item.roleName:'待补充信息'}}</view>
					</view>
				</view>
				<view style="height: 80rpx;"></view>
			</view>
			<view v-if="userinfo.usertype" class="wid90 mar dis disAl disJuB foSi35 mar-top30">
				<view class="hei80 dis disAl disJuC borRad10 bacFFF" :style="'color: '+back+';'" @click="fenxiang"
					style="width: 300rpx;">
					{{list.state == 'INIT' ? '去签署' : ''}}
					{{list.state == 'PART' ? '去签署' : ''}}
					{{list.state == 'ALL' ? '查看合同' : ''}}
					{{list.state == 'REJECT' ? '查看合同' : ''}}
					{{list.state == 'CANCEL' ? '查看合同' : ''}}
					{{list.state == 'WILLEXPIRE' ? '查看合同' : ''}}
					{{list.state == 'DEADLINE' ? '查看合同' : ''}}
					{{list.state == 'RELIEVED' ? '查看合同' : ''}}
					{{list.state == 'EXCEPTION' ? '查看合同' : ''}}
					</view>
				<view class="hei80 dis disAl disJuC borRad10 bacFFF" :style="'color: '+back+';'" @click="buchong"
					style="width: 300rpx;">补充信息</view>
			</view>
			<view v-if="!userinfo.usertype" class="wid90 mar dis disAl disJuC foSi35 mar-top30">
				<view class="hei80 wid100 dis disAl disJuC borRad10 bacFFF" :style="'color: '+back+';'"
					@click="buchong">
					{{list.state == 'INIT' ? '去签署' : ''}}
					{{list.state == 'PART' ? '去签署' : ''}}
					{{list.state == 'ALL' ? '查看合同' : ''}}
					{{list.state == 'REJECT' ? '查看合同' : ''}}
					{{list.state == 'CANCEL' ? '查看合同' : ''}}
					{{list.state == 'WILLEXPIRE' ? '查看合同' : ''}}
					{{list.state == 'DEADLINE' ? '查看合同' : ''}}
					{{list.state == 'RELIEVED' ? '查看合同' : ''}}
					{{list.state == 'EXCEPTION' ? '查看合同' : ''}}
					</view>
			</view>
			<view class="wid90 mar dis disAl disJuC hei80 foSi25" :style="'color: '+back+';'"
				@click="tiaozhuan('/pages/template/particularsContract?flowid='+list.flowId)">
				<view>查看合同详情</view>
				<uni-icons size="20" :color="back" type="right"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	import tabnav from "../common/tabnav/tabnav.vue"
	export default {
		components: {
			tabnav,
		},
		data() {
			return {
				aurl: this.global.url,
				back: uni.getStorageSync('back'),
				backTwo: uni.getStorageSync('backTwo'),
				pdfurl: '',
				tops: '',
				erImage: '',
				list: {},
				flowId: '',
				icon_list: {
					jianyan: '/addon/qianyu_dzq/wechatimg/jianyan.png'
				}
			}
		},
		onLoad(option) {
			let that = this
			that.id = option.id
			this.userinfo = uni.getStorageSync('inuse_userinfo');
			console.log('userinfo=', this.userinfo)
		},
		onShow() {
			let that = this
			this.pdfurl = uni.getStorageSync('contract_detail')
			console.log('pdfurl=', this.pdfurl)
			that.getList()
			that.getMobile()
		},
		created() {
			uni.getSystemInfo({
				success: (e) => {
					console.log(e)
					this.tops = e.statusBarHeight
				}
			})
		},
		methods: {
			saveewm() {
				uni.showLoading({
					title: '加载中...'
				});
				//wx.downloadFile方法：下载文件资源到本地
				uni.downloadFile({
					url: this.erImage, //图片地址
					success: function(res) {
						console.log(111, res);
						//wx.saveImageToPhotosAlbum方法：保存图片到系统相册
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath, //图片文件路径
							success: function(data) {
								uni.hideLoading(); //隐藏 loading 提示框
								uni.showModal({
									title: '提示',
									content: '保存成功',
									modalType: false,
								})
							},
							// 接口调用失败的回调函数
							fail: function(err) {
								if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err
									.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err
									.errMsg === "saveImageToPhotosAlbum:fail authorize no response"
								) {
									uni.showModal({
										title: '提示',
										content: '需要您授权保存相册',
										modalType: false,
										success: modalSuccess => {
											uni.openSetting({
												success(settingdata) {
													console.log("settingdata",
														settingdata)
													if (settingdata
														.authSetting[
															'scope.writePhotosAlbum'
														]) {
														uni.showModal({
															title: '提示',
															content: '获取权限成功,再次点击图片即可保存',
															modalType: false,
														})
													} else {
														uni.showModal({
															title: '提示',
															content: '获取权限失败，将无法保存到相册哦~',
															modalType: false,
														})
													}
												},
												fail(failData) {
													console.log("failData",
														failData)
												},
												complete(finishData) {
													console.log("finishData",
														finishData)
												}
											})
										}
									})
								}
							},
							complete(res) {
								uni.hideLoading(); //隐藏 loading 提示框
							}
						})
					}
				})
			},

			cun() {
				let that = this
				uni.request({
					url: that.global.url + 'api/qianyu_dzq/applet_save_flowid_to_did',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						'token': uni.getStorageSync('inuse_userinfo_token')
					},
					data: {
						flowId: uni.getStorageSync('flowId')
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							console.log(result.data)
							that.erweima(result.data)
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none',
								duration: 2000
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			erweima(id) {
				let that = this
				var path = 'pages/index/index'
				uni.request({
					url: that.global.url + 'api/qianyu_dzq/applet_contracts_qrcode',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						'token': uni.getStorageSync('inuse_userinfo_token')
					},
					data: {
						page: path,
						scene: id
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							console.log(result.data)
							that.erImage = result.data
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none',
								duration: 2000
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			fenxiang() {
				let that = this
				wx.downloadFile({
					url: that.erImage,
					success: (res) => {
						wx.showShareImageMenu({
							path: res.tempFilePath
						})
					}
				})
			},
			getMobile() {
				let that = this
				uni.request({
					url: that.global.url + 'api/qianyu_dzq/applet_token_to_userinfo',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						'token': uni.getStorageSync('inuse_userinfo_token')
					},
					data: {
						type: 1
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							console.log(result.data)
							that.mobile = result.data.mobile
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none',
								duration: 2000
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			getList() {
				let that = this
				uni.request({
					url: that.global.url + 'api/qianyu_dzq/applet_current_details',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						'token': uni.getStorageSync('inuse_userinfo_token')
					},
					data: {
						flowid: uni.getStorageSync('flowId')
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							console.log('detail')
							console.log(result.data)
							that.list = result.data
							that.cun()
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none',
								duration: 2000
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			buchong() {
				let that = this
				uni.request({
					url: that.global.url + 'api/qianyu_dzq/applet_contracts_createsignurls_public',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						'token': uni.getStorageSync('inuse_userinfo_token')
					},
					data: {
						flowid: uni.getStorageSync('flowId')
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							console.log(result.data)
							var data = result.data
							for (var i = 0; i < data.length; i++) {
								if (that.mobile == data[i].mobile) {
									that.goNativeindex(data[i].signUrl)
								}
							}
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none',
								duration: 2000
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			goNativeindex(path) {
				uni.openEmbeddedMiniProgram({
					appId: 'wxa023b292fd19d41d', // 此为 appid
					path: path, // 此为路径
					envVersion: "release",
					success: res => {
						// 打开成功
						console.log("打开成功", res);
					},
					fail: err => {
						console.log(err);
					}
				})
			},
			tiaozhuan(url) {
				uni.navigateTo({
					url: url
				});
			},
			shouyin() {
				uni.switchTab({
					url: '/pages/index/index'
				});
			},
		}
	}
</script>

<style>
	.content {
		min-height: 100vh;
		background-color: #F4F3F3;
	}
</style>